<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */


// Call WeekendsTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
    define("PHPUnit_MAIN_METHOD", "WeekendsTest::main");
}

require_once "PHPUnit/Framework/TestCase.php";
require_once "PHPUnit/Framework/TestSuite.php";

require_once "testConf.php";

$_SESSION['WPATH'] = WPATH;

require_once 'Weekends.php';
require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
require_once ROOT_PATH."/lib/confs/Conf.php";

/**
 * Test class for Weekends.
 * Generated by PHPUnit_Util_Skeleton on 2007-01-05 at 05:42:38.
 */
class WeekendsTest extends PHPUnit_Framework_TestCase {
    public $classWeekend = null;
    public $connection = null;
	/**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit/TextUI/TestRunner.php";

        $suite  = new PHPUnit_Framework_TestSuite("WeekendsTest");
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {
		$this->classWeekend = new Weekends();

    	$conf = new Conf();

    	$this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);

    	mysql_select_db($conf->dbname);
    	mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (1, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (2, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (3, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (4, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (5, ".Weekends::WEEKENDS_LENGTH_FULL_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (6, ".Weekends::WEEKENDS_LENGTH_HALF_DAY.");");
    	mysql_query("INSERT INTO `".Weekends::WEEKENDS_TABLE."` (`".Weekends::WEEKENDS_TABLE_DAY."`, `".Weekends::WEEKENDS_TABLE_LENGTH ."`) VALUES (7, ".Weekends::WEEKENDS_LENGTH_WEEKEND.");");

        mysql_query("INSERT INTO `hs_hr_employee`(emp_number, employee_id, emp_lastname, emp_firstname, emp_middle_name, emp_nick_name) " .
                "VALUES (18, NULL, 'Nadeeth', 'H', 'Lansakara', 'rc')");
        mysql_query("INSERT INTO `hs_hr_leavetype` VALUES ('LTY010', 'Medical', 1)");
        mysql_query("INSERT INTO `hs_hr_leave_requests` (`leave_request_id`, `leave_type_id`, `leave_type_name`, `date_applied`, `employee_id`) VALUES (11, 'LTY010', 'Medical', '".date('Y-m-d', time()+3600*24)."', '18')");
        mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (10, '18', 'LTY010', '2009-02-07', 4, 0.5, 3, '', 11)");
        mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (11, '18', 'LTY010', '2009-02-08', 8, 1, 3, '', 11)");
        mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (12, '18', 'LTY010', '2009-02-09', 8, 1, 4, '', 11)");
        mysql_query("INSERT INTO `hs_hr_leave` (`leave_id`, `employee_id`, `leave_type_id`, `leave_date`, `leave_length_hours`, `leave_length_days`, `leave_status`, `leave_comments`, `leave_request_id`) VALUES (13, '18', 'LTY010', '2009-02-10', 8, 1, 4, '', 11)");
    }

    /**
     * Tears down the fixture, for example, close a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {
    	mysql_query("TRUNCATE TABLE `".Weekends::WEEKENDS_TABLE."`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_leave`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_employee`", $this->connection);
        mysql_query("TRUNCATE TABLE `hs_hr_leave_requests`", $this->connection);
    }

    /**
     * @todo Implement testFetchWeek().
     */
    public function testFetchWeek() {
    	$expected[0] = array(1, 0);
    	$expected[1] = array(2, 0);
    	$expected[2] = array(3, 0);
    	$expected[3] = array(4, 0);
    	$expected[4] = array(5, 0);
    	$expected[5] = array(6, 4);
    	$expected[6] = array(7, 8);

        $res = $this->classWeekend->fetchWeek();

        $this->assertNotNull($res, 'Unexpected behavior');
        $this->assertEquals(count($res), 7, 'Invalid number of records found');

        for ($i=0; $i<count($res); $i++) {
        	$this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
        	$this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
        }

    }

    /**
     * @todo Implement testEditDay().
     */
    public function testEditDay() {

        $expected[0] = array(1, 0);
    	$expected[1] = array(2, 0);
    	$expected[2] = array(3, 0);
    	$expected[3] = array(4, 4);
    	$expected[4] = array(5, 8);
    	$expected[5] = array(6, 0);
    	$expected[6] = array(7, 0);

    	for ($i=0; $i<count($expected); $i++) {
	    	$this->classWeekend->setDay($expected[$i][0]);
	    	$this->classWeekend->setLength($expected[$i][1]);

	    	$this->classWeekend->editDay();
    	}

        $res = $this->classWeekend->fetchWeek();

        $this->assertNotNull($res, 'Unexpected behavior');
        $this->assertEquals(count($res), 7, 'Invalid number of records found');

        for ($i=0; $i<count($res); $i++) {
        	$this->assertEquals($res[$i]->getDay(), $expected[$i][0], 'Invalid day');
        	$this->assertEquals($res[$i]->getLength(), $expected[$i][1], 'Invalid length');
        }
    }

    public function testIsWeekend() {
		$this->assertTrue(Weekends::isWeekend("2008-07-27"));
		$this->assertFalse(Weekends::isWeekend("2008-07-26"));
		$this->assertFalse(Weekends::isWeekend("2008-07-25"));
    }

    public function testUpdateWeekendsForLeaves(){
        Weekends::updateWeekendsForLeaves();
        $dbConnection = new DMLFunctions();
        $query = "SELECT leave_id, leave_status FROM hs_hr_leave ";
        $result = $dbConnection -> executeQuery($query);
        $leaves = array();
        while($row = $dbConnection->dbObject->getArray($result)){
            $leaves[$row['leave_id']] = $row['leave_status'];
        }
        $this->assertEquals(4, $leaves['10'], 'Invalid status');
        $this->assertEquals(4, $leaves['11'], 'Invalid status');
        $this->assertEquals(3, $leaves['12'], 'Invalid status');
        $this->assertEquals(3, $leaves['13'], 'Invalid status');
    }

    public function testGetWeekendLength(){
        $sunday = Weekends::getWeekendLength('2009-02-15');
        $saturday = Weekends::getWeekendLength('2009-02-14');
        $friday = Weekends::getWeekendLength('2009-02-13');
        $this->assertEquals(8, $sunday, 'Invalid length');
        $this->assertEquals(4, $saturday, 'Invalid Length');
        $this->assertEquals(0, $friday, 'Invalid Length');
    }
}

// Call WeekendsTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "WeekendsTest::main") {
    WeekendsTest::main();
}
?>
